import json
# nums = input("请输入你喜欢的数字：")
# with open('D:\\Python\\projects\\python_01\\files\\numbers.json','w') as w_num:
#     json.dump(nums,w_num)
#     print("数字已成功写入json文件！")
# with open('D:\\Python\\projects\\python_01\\files\\numbers.json','r') as r_num:
#     f_nums = json.load(r_num)
# print("I know your favorite number! It’s "+str(f_nums))


filename = 'D:\\Python\\projects\\python_01\\files\\numbers.json'
try:
    with open(filename,) as user_num:
        num = json.load(user_num)
except ValueError:
    num = input("请输入你喜欢的数字：")
    with open(filename,'w') as user_num:
        json.dump(num,user_num)
        print("We'll remember your favorite number when you come back, "+str(num)+" !")
else:
    print("Your favorite number is "+str(num)+" !")


def get_stored_username():
    """如果存储了用户名，就获取它"""
    filename = 'D:\\Python\\projects\\python_01\\files\\username.json'
    try:
        with open(filename) as file_object:
            username = json.load(file_object)
    except ValueError:
        return None
    else:
        return (username)

def get_new_username():
    """提示用户输入用户名"""
    username = input("What's your name?")
    filename = '/files/username.json'
    with open(filename, 'w') as file_object:
        json.dump(username, file_object)
    return username

def greet_user():
    username = get_stored_username()
    if username:
        ask = input("Is the name [" + username + "] right?(y/n)")
        if ask == 'y':
            print("Welcome back," + username + " !")
        else:
            username = get_new_username()
            print("We'll remember you," + username + " !")
    else:
        username = get_new_username()
        print("We'll remember you," + username + " !")

greet_user()
